package com.liveinstars.account.business.util;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

public class AESPKCS7Util {
    private static final String AES = "AES/ECB/PKCS7Padding";
    public static final String CRYPT_KEY = "WTAHAPPYACTIVITY";
    private static final String ENCRYPT_CHARSET = "UTF-8";

    public AESPKCS7Util() {
    }

    public static void main(String[] args) throws Exception {
        String content = "hello World";
        String password = "151";
        System.out.println("原内容：\n" + content);
        String tempContent = encrypt(content, "WTAHAPPYACTIVITY");
        System.err.println("加密后：\n" + tempContent);
        tempContent = decrypt(tempContent, "WTAHAPPYACTIVITY");
        System.out.println("解密后：\n" + tempContent);
    }

    public static String encrypt(String input, String key) {
        byte[] crypted = null;

        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            cipher.init(1, skey);
            crypted = cipher.doFinal(input.getBytes());
        } catch (Exception var5) {
            System.out.println(var5.toString());
            var5.printStackTrace();
        }

        return new String(Base64.encodeBase64(crypted));
    }

    public static String decrypt(String input, String key) {
        byte[] output = null;

        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            cipher.init(2, skey);
            output = cipher.doFinal(Base64.decodeBase64(input));
        } catch (Exception var5) {
            System.out.println(var5.toString());
        }

        return new String(output);
    }

    static {
        Security.addProvider(new BouncyCastleProvider());
    }
}
